package com.phuongtn2.karaoke.activity;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.phuongtn2.karaoke.R;
import com.phuongtn2.karaoke.adapter.SettingsRoomAdapter;
import com.phuongtn2.karaoke.database.DatabaseHelper;
import com.phuongtn2.karaoke.database.dto.RoomDto;

public class AddRoomActivity extends Activity implements OnClickListener {

	public List<RoomDto> dtos = new ArrayList<RoomDto>();
	public DatabaseHelper databaseHelper;
	public RoomDto roomDto;
	public Button btAddRoomAtEditScreen;
	public Button btSaveRoomAtEditScreen;
	public Intent intent;
	public String type;
	public SettingsRoomAdapter settingsRoomAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.room_edit);
		intent = getIntent();
		type = intent.getStringExtra("type");
		btAddRoomAtEditScreen = (Button) findViewById(R.id.btAddRoomAtEditScreen);
		btAddRoomAtEditScreen.setOnClickListener(this);
		btSaveRoomAtEditScreen = (Button) findViewById(R.id.btSaveRoom);
		if (type.equalsIgnoreCase("add")) {
			btAddRoomAtEditScreen.setVisibility(View.VISIBLE);
			btSaveRoomAtEditScreen.setVisibility(View.INVISIBLE);
		} else {
			btAddRoomAtEditScreen.setVisibility(View.INVISIBLE);
			btSaveRoomAtEditScreen.setVisibility(View.VISIBLE);
		}
		databaseHelper = new DatabaseHelper(getApplicationContext());
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v != null) {
			switch (v.getId()) {
			case R.id.btAddRoomAtEditScreen:
				roomDto = new RoomDto();
				EditText edtRoomName = (EditText) findViewById(R.id.edtRoomName);
				EditText edtRoomRemark = (EditText) findViewById(R.id.edtRemark);
				RadioButton rbActive = (RadioButton) findViewById(R.id.roomActive);
				RadioButton rbInactive = (RadioButton) findViewById(R.id.roomInactive);
				if (!edtRoomName.toString().equalsIgnoreCase("")) {
					roomDto.setRoomName(edtRoomName.getText().toString());
					roomDto.setRoomRemark(edtRoomRemark.getText().toString());
					if (rbActive.isChecked()) {
						roomDto.setRoomStatus(rbActive.getTag().toString());
					} else {
						roomDto.setRoomStatus(rbInactive.getTag().toString());
					}
					databaseHelper.insertRoom(roomDto);
					Toast.makeText(getApplicationContext(),
							"Insert room success!!!", Toast.LENGTH_LONG).show();
					edtRoomName.setText("");
					edtRoomRemark.setText("");
					rbActive.setChecked(true);
				} else {
					Toast.makeText(getApplicationContext(),
							"Please input room name!!!", Toast.LENGTH_LONG)
							.show();
				}
				break;
			default:
				break;
			}
		} else {
			return;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.home, menu);
		return true;
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			databaseHelper = new DatabaseHelper(getApplicationContext());
			dtos = databaseHelper.getAllRoom();
			if (dtos.size() > 0) {
				settingsRoomAdapter  = new SettingsRoomAdapter(
						getApplicationContext(), dtos,this);
				ListView listView = (ListView) findViewById(R.id.roomListView);	
				listView.setAdapter(settingsRoomAdapter);
				//roomNameAdapter.notifyDataSetChanged();
			}
			return true;
		}
		return false;

	}
}
